package com.ruoyi.web.controller.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.system.domain.CNews;
import com.ruoyi.system.service.ICNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 微信公众号菜单 信息操作处理
 * 
 * @author liupan
 * @date 2019-03-25
 */
@Api(tags = "信息资讯接口")
@RestController
@RequestMapping("/rest/information")
public class _InformationController extends BaseController {
	@Autowired
	private ICNewsService icNewsService;

	/**
	 * 文件上传路径
	 */
	public static final String UPLOAD_PATH = "/profile/upload/";

	@ApiOperation(value = "获取广告图",notes = "27:首页头部，28:资讯页头部")
	@GetMapping("banner/{type}")
	public AjaxResult getBanner(@PathVariable("type") Integer type) {
		if(type == null){
			return AjaxResult.error("类型不能为空");
		}
		Object cacheObj = CacheUtils.get(Constants.BANNER_KEY, Constants.BANNER_KEY+":"+type);
		List<Map<String,Object>> news = null;
		if(cacheObj != null){
			news = (List<Map<String,Object>>)cacheObj;
		}else{
			news = icNewsService.listMaps(new QueryWrapper<CNews>()
					.eq("cate_id",type)
					.eq("news_status",0)
					.select("id","images","title").orderByDesc("create_time"));
			if(CollectionUtils.isNotEmpty(news)){
				CacheUtils.put(Constants.BANNER_KEY, Constants.BANNER_KEY+":"+type, news);
			}
		}
		return AjaxResult.success(news);
	}

	@ApiOperation(value = "获取新闻",notes = "19:公司简介，20:产品介绍,22:案例分享,29:门店风采,30:养生知识")
	@GetMapping("news/{type}")
	public AjaxResult getNews(@PathVariable("type") Integer type) {
		if(type == null){
			return AjaxResult.error("类型不能为空");
		}
		List<Map<String,Object>> news = icNewsService.listMaps(new QueryWrapper<CNews>()
				.eq("cate_id",type)
				.eq("news_status",0)
				.select("id,images,title,create_time").orderByDesc("create_time"));

		return AjaxResult.success(news);
	}

	@ApiOperation(value = "新闻详细",notes = "19:公司简介，20:产品介绍,22:案例分享,29:门店风采,30:养生知识")
	@GetMapping("detail/{id}")
	public AjaxResult detail(@PathVariable("id") Integer id) {
		if(id == null){
			return AjaxResult.error("id不能为空");
		}
		CNews news = icNewsService.getById(id);
		return AjaxResult.success(news);
	}



}
